'use strict';

let util = require('util');
let eventEmitter = require('events').EventEmitter;
let fs = require('fs')

function InputChecker (name, file){
  this.name = name;
  this.writeStream = fs.createWriteStream('./' + file + '.txt', {
    'flags' : 'a',
    'encoding': 'utf8',
    'mode': 0o666
  })
}

util.inherits( InputChecker, eventEmitter );

InputChecker.prototype.check = function check(input) {
  // trim extraneouts white sapce
  let command = input.trim().substr(0, 3);

  // process command
  // if wr, write input to file
  if(command === 'wr:') {
    this.emit('write', input.substr(3, input.length));

    // if en, end process
  }else if ( command === 'en:'){
    this.emit('end');
  }else {
    this.emit('echo', input);
  }
}

// testing new dobject and ecent handling
let ic = new InputChecker('shelley', 'output');

ic.on('write', (data) => {
  ic.writeStream.write(data, 'utf8');
})

ic.on( 'echo', (data) => {
  process.stdout.write(ic.name + ' Wrote ' + data);
})

ic.on('end', () => {
  process.exit();
})

// caputer input after setting encoding
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
  let input = process.stdin.read()
  if(input !== null){
    ic.check(input);
  }
})


